<?php
class Dieta_model extends CI_Model {
	function ingrediencie($slug) {
		$query = $this->db->select('Nazov')
				->where('Dieta', $slug)
				->get('ZakazanaIngrediencia');
				
		return $query->result_array();
	}
	
	function vymazanie_diety($dieta) {
		$this->db->delete('ZakazanaIngrediencia', array('Dieta' => $dieta));
	}
	
	function pridanie_ingrediencii($param) {
		foreach (explode("\n", $param) as $ingrediencia) {
			$query = $this->db->where('Nazov', $ingrediencia)
							->get('Ingrediencia');
			if ($query->num_rows == 0) {
				$data = array(
				   'Nazov' => $ingrediencia
				);
				
				$this->db->insert('Ingrediencia', $data); 
			}
		}
	}
	
	function ingrediencie_do_diety($param, $dieta) {
		foreach (explode("\n", $param) as $ingrediencia) {
			$data = array(
				'Dieta' => $dieta,
				'Nazov' => $ingrediencia
			);
			$this->db->insert('ZakazanaIngrediencia', $data);
		}
	}
	
	function nova_dieta($slug) {
		
		$data = array(
			'Meno' => $slug,
			'Popis' => 'bez popisu'
		);
		$this->db->insert('Dieta', $data);
	}
	
	function ziskaj_nazov($slug) {
		$query = $this->db->where('ID', $slug)->get('Dieta');
		$ret = $query->row_array();
		return $ret['Meno'];
	}
	
	function ziskaj_id($slug) {
		$query = $this->db->where('Meno', $slug)->get('Dieta');
		$ret = $query->row_array();
		return $ret['ID'];
	}
}